package com.example.dllo.my36kr.find.activities;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.news.NewsAdapter;
import com.example.dllo.my36kr.tools.BaseAty;
import com.example.dllo.my36kr.tools.NetTool;
import com.example.dllo.my36kr.tools.StaticUrl;

/**
 * Created by dllo on 16/8/25.
 * 近期活动
 */
public class NearTermActivity extends BaseAty {


    private ListView listView;
    private NearAdapter adapter;
    private LinearLayout layout;
    private AllWindow allWindow;
    private NearReceiver mReceiver;

    @Override
    protected int setLayout() {
        return R.layout.activity_nearterm;
    }

    @Override
    protected void initView() {
        listView = bindView(R.id.lv_activity_nearterm);//listView
        adapter = new NearAdapter(getApplicationContext());//适配器
        layout = bindView(R.id.ll_near_below);//活动类型的Id
        ImageButton back = bindView(R.id.ib_find_near_back);//返回键
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Button huodongBnt = bindView(R.id.bnt_near_huodong);//活动类型点击事件
        huodongBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                allWindow = new AllWindow(NearTermActivity.this, itemsOnClick);//活动类型下的PopupWindow
                allWindow.showAsDropDown(layout);//活动类型的下面显示
            }
        });

    }

    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            allWindow.dismiss();//SecondWindow 点击关闭

        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        //广播
        mReceiver = new NearReceiver();//广播接收器
        IntentFilter filter = new IntentFilter();
        filter.addAction("near");
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(mReceiver);//关闭广播
    }

    @Override
    protected void initData() {
        //解析
        mNetTool.getNetData(StaticUrl.FIND_NEAR_TERM_URL, NearBean.class, new NetTool.NetListener<NearBean>() {
            @Override
            public void onSuccess(final NearBean nearBean) {
                adapter.setNearBean(nearBean);
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //获取网址直接跳转网页
                        Uri uri = Uri.parse(nearBean.getData().getData().get(position).getActivityLink());
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);
                    }
                });
            }
            @Override
            public void onError(String errorMsg) {

            }
        });


    }

    public class AllWindow extends PopupWindow implements View.OnClickListener {//PopupWindow
        Context mContext;

        public AllWindow(Context context, View.OnClickListener itemsOnClick) {
            super(context);
            this.mContext = context;
            View view = LayoutInflater.from(context).inflate(R.layout.all_window, null);
            TextView allTv = (TextView) view.findViewById(R.id.tv_newr_all);
            TextView dayTv = (TextView) view.findViewById(R.id.tv_newr_day);
            TextView kongjianTv = (TextView) view.findViewById(R.id.tv_newr_kongjian);
            TextView touziTv = (TextView) view.findViewById(R.id.tv_newr_touzi);
            TextView fuwuTv = (TextView) view.findViewById(R.id.tv_newr_fuwu);
            TextView rongziTv = (TextView) view.findViewById(R.id.tv_newr_rongzi);
            allTv.setOnClickListener(this);
            dayTv.setOnClickListener(this);
            kongjianTv.setOnClickListener(this);
            touziTv.setOnClickListener(this);
            fuwuTv.setOnClickListener(this);
            rongziTv.setOnClickListener(this);
            this.setContentView(view);
            this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
            //设置SelectPicPopupWindow弹出窗体的高
            this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
            //设置SelectPicPopupWindow弹出窗体可点击
            this.setFocusable(true);//窗口可以点击
            ColorDrawable dw = new ColorDrawable(0xb0000000);//设置窗口的颜色
            this.setBackgroundDrawable(dw);
            view.setOnTouchListener(new View.OnTouchListener() {//点击其他地方消失
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    dismiss();
                    return false;
                }
            });

        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();//PopupWindow发广播
            intent.setAction("near");
            switch (v.getId()) {
                case R.id.tv_newr_all:
                    intent.putExtra("near", StaticUrl.FIND_NEAR_TERM_URL);
                    break;
                case R.id.tv_newr_day:
                    intent.putExtra("near", StaticUrl.FIND_NEAR_DAY_URL);
                    break;
                case R.id.tv_newr_kongjian:
                    intent.putExtra("near", StaticUrl.FIND_NEAR_KJ_URL);
                    break;
                case R.id.tv_newr_touzi:
                    intent.putExtra("near", StaticUrl.FIND_NEAR_TZ_URL);
                    break;
                case R.id.tv_newr_fuwu:
                    intent.putExtra("near", StaticUrl.FIND_NEAR_FW_URL);
                    break;
                case R.id.tv_newr_rongzi:
                    intent.putExtra("near", StaticUrl.FIND_NEAR_RZ_URL);
                    break;
            }
            mContext.sendBroadcast(intent);
            dismiss();//点击消失

        }
    }

    private class NearReceiver extends BroadcastReceiver {//广播
        @Override
        public void onReceive(Context context, Intent intent) {
            String near = intent.getStringExtra("near");
            mNetTool.getNetData(near, NearBean.class, new NetTool.NetListener<NearBean>() {
                @Override
                public void onSuccess(final NearBean nearBean) {
                    adapter.setNearBean(nearBean);
                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Uri uri = Uri.parse(nearBean.getData().getData().get(position).getActivityLink());
                            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                            startActivity(intent);
                        }
                    });
                }

                @Override
                public void onError(String errorMsg) {

                }
            });

        }
    }
}
